package com.lishu.carsafe.constant;

import android.content.Context;
import android.content.SharedPreferences;

public class UserPreferences {
    private static UserPreferences mUserPreferences;
    private SharedPreferences sharePreferences;

    private final String USER_ID = "user_id";
    private final String USER_NAME = "user_name";
    private final String LOGIN_NAME = "login_name";
    private final String LOGIN_PASSWORD = "login_password";
    private final String SESSION_ID = "session_id";

    public static UserPreferences getInstance() {
        if (mUserPreferences == null) {
            mUserPreferences = new UserPreferences();
        }
        return mUserPreferences;
    }

    public void init(Context context) {
        if (sharePreferences == null) {
            sharePreferences = context.getSharedPreferences("safe", Context.MODE_PRIVATE);
        }
    }

    /**
     * 用户Id
     */
    /*public void setUserId(int userId) {
        if (sharePreferences != null) {
            sharePreferences.edit().putInt(USER_ID, userId).commit();
        }
    }

    public int getUserId() {
        if (sharePreferences != null) {
            return sharePreferences.getInt(USER_ID, 0);
        }
        return 0;
    }*/
    public void setUserId(String userId) {
        if (sharePreferences != null) {
            sharePreferences.edit().putString(USER_ID, userId).commit();
        }
    }

    public String getUserId() {
        if (sharePreferences != null) {
            return sharePreferences.getString(USER_ID, "0");
        }
        return "";
    }

    //用户名
    public void setUserName(String userName) {
        if (sharePreferences != null) {
            sharePreferences.edit().putString(USER_NAME, userName).commit();
        }
    }

    public String getUserName() {
        if (sharePreferences != null) {
            return sharePreferences.getString(USER_NAME, "");
        }
        return "";
    }

    /**
     * 登录账号
     */
    public void setLoginName(String loginName) {
        if (sharePreferences != null) {
            sharePreferences.edit().putString(LOGIN_NAME, loginName).commit();
        }
    }

    public String getLoginName() {
        if (sharePreferences != null) {
            return sharePreferences.getString(LOGIN_NAME, "");
        }
        return "";
    }
    /**
     * 密码
     */
    public void setLoginPassword(String loginPassword) {
        if (sharePreferences != null) {
            sharePreferences.edit().putString(LOGIN_PASSWORD, loginPassword).commit();
        }
    }

    public String getLoginPassword() {
        if (sharePreferences != null) {
            return sharePreferences.getString(LOGIN_PASSWORD, "");
        }
        return "";
    }
    /**
     * Session
     */
    public void setSessionId(String sessionId) {
        if (sharePreferences != null) {
            sharePreferences.edit().putString(SESSION_ID, sessionId).commit();
        }
    }

    public String getSessionId() {
        if (sharePreferences != null) {
            return sharePreferences.getString(SESSION_ID, "");
        }
        return "";
    }


    /**
     * 清除登录信息
     */
    public void logout() {
        if (sharePreferences != null) {
            SharedPreferences.Editor editor = sharePreferences.edit();
            editor.remove(USER_ID);
            editor.remove(LOGIN_PASSWORD);
            editor.remove(USER_NAME);
            editor.remove(LOGIN_NAME);
            editor.remove(SESSION_ID);

            editor.commit();
        }
    }
}
